//
//  RYCToolBar.m
//  TestTool
//
//  Created by zhiwei ma on 12-8-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RYCToolBar.h"
#import <QuartzCore/QuartzCore.h>

#define RYCToolBarItemTitleColorNormal      [UIColor colorWithRed:102/255.0f green:94/255.0f blue:94/255.0f alpha:1]
#define RYCToolBarItemTitleColorSelected    [UIColor colorWithRed:222/255.0f green:148/255.0f blue:111/255.0f alpha:1]

@interface RYCToolBar ()
{
    UIImageView* _itemBgView;
    UIViewContentMode _itemBgViewContentMode;
    id<RYCToolBarDelegate> _toolbardelegate;
}
@property (nonatomic, retain) UIScrollView* scrollView;
@end

@implementation RYCToolBar
@synthesize scrollView = _scrollView;
@synthesize items = _items;
@synthesize selectedIndex = _selectedIndex;
@synthesize bgImageView = _bgImageView;
@synthesize itemBackgroundImage = _itemBackgroundImage;
@synthesize itemBackgroundColor = _itemBackgroundColor;
@synthesize toolbardelegate = _toolbardelegate;
@synthesize itemAlign = _itemAlign;
@synthesize itemGap = _itemGap;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _itemAlign = 3;
        _itemGap = 3;
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:_scrollView];
    }
    return self;
}

- (void)dealloc
{
    [_items release];
    self.scrollView = nil;
    self.bgImageView = nil;
    self.itemBackgroundColor = nil;
    self.itemBackgroundImage = nil;
    [super dealloc];
}

- (void)setItems:(NSArray*)aItems
{
    [_scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if (_itemBgView)
    {
        [_itemBgView release];
        _itemBgView = nil;
    }

    
    [_items release];
    _items = nil;
    
    _items = [NSArray arrayWithArray:aItems];
    [_items retain];
    
    CGFloat contentWidth = _itemAlign*2 - _itemGap;
    CGPoint itemTL;
    itemTL.x = _itemAlign;

    for (UIButton* itemView in aItems)
    {
        CGRect rect = itemView.frame;
        itemTL.y = (self.bounds.size.height - rect.size.height) / 2;
        rect.origin = itemTL;
        itemView.frame = rect;
        
        [itemView addTarget:self action:@selector(itemClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:itemView];
        
        itemTL.x += rect.size.width + _itemGap;
        contentWidth += rect.size.width + _itemGap;
    }
    _scrollView.contentSize = CGSizeMake(contentWidth, self.bounds.size.height);
}

- (UIImageView*)bgImageView
{
    if (nil == _bgImageView)
    {
        _bgImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self insertSubview:_bgImageView belowSubview:_scrollView];
    }
    return _bgImageView;
}

- (void)setBgItemImageViewContentMode:(UIViewContentMode)contentMode
{
    _itemBgViewContentMode = contentMode;
}

- (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated
{
    if (selectedIndex >= 0 && selectedIndex < [_items count])//参数有效性判断
    {
        CGRect oldRect = CGRectZero;
        if (_selectedIndex >= 0)
        {
            UIButton* old = [_items objectAtIndex:_selectedIndex];
            [old setTitleColor:RYCToolBarItemTitleColorNormal forState:UIControlStateNormal];
            oldRect = old.frame;
        }
        
        _selectedIndex = selectedIndex;
        UIButton* new = [_items objectAtIndex:selectedIndex];
        [new setTitleColor:RYCToolBarItemTitleColorSelected forState:UIControlStateNormal];

        CGRect newRect = new.frame;
        if (animated)
        {
            if (nil == _itemBgView)
            {
                _itemBgView = [[UIImageView alloc] initWithFrame:CGRectZero];
                _itemBgView.contentMode = _itemBgViewContentMode;
                CALayer* layer = _itemBgView.layer;
                layer.cornerRadius = 5;
                if (_itemBackgroundImage)
                {
                    _itemBgView.image = _itemBackgroundImage;
                }
                if (_itemBackgroundColor)
                {
                    _itemBgView.backgroundColor = _itemBackgroundColor;
                }
                [_scrollView insertSubview:_itemBgView atIndex:0];
            }
            else
            {
                [_scrollView sendSubviewToBack:_itemBgView];
            }
            _itemBgView.frame = oldRect;
            
            [UIView beginAnimations:@"bg" context:nil];
            [UIView setAnimationDuration:0.3];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
            
            _itemBgView.frame = newRect;
            [UIView commitAnimations];
        }

    }
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

- (void)itemClicked:(id)aSender
{
    NSInteger clickedIndex = 0;
    for (NSInteger i = 0; i < [_items count]; i++)
    {
        UIButton* button = [_items objectAtIndex:i];
        if (aSender == button)
        {
            clickedIndex = i;
            break;
        }
    }
    if (clickedIndex == _selectedIndex)
    {
        return;
    }
    
    [self setSelectedIndex:clickedIndex animated:YES];
    if (_toolbardelegate && [_toolbardelegate respondsToSelector:@selector(didItemSelected:selectedIndex:)])
    {
        [_toolbardelegate didItemSelected:self selectedIndex:clickedIndex];
    }
}
@end

@implementation UIButton (RYCToolbar)
+ (UIButton*)buttonWithTitle:(NSString*)aTitle
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];

    UIFont* font = [UIFont systemFontOfSize:14.0f];
    CGSize size = [aTitle sizeWithFont:font];
    
    button.frame = CGRectMake(0, 0, size.width+20, size.height+8);
    [button setTitle:aTitle forState:UIControlStateNormal];
    [button setTitleColor:RYCToolBarItemTitleColorNormal forState:UIControlStateNormal];
    button.titleLabel.font = font;
    return button;
}
@end